/*******************************************************************************
 * 名称： 1103. 分糖果 II
 * 地址： https://leetcode.cn/problems/distribute-candies-to-people/description/?envType=daily-question&envId=2024-06-03
 ******************************************************************************/

/**
 * @param {number} candies
 * @param {number} num_people
 * @return {number[]}
 */
var distributeCandies = function (candies, num_people) {
  const resArr = new Array(num_people).fill(0);
  let haveCandies = candies;
  let peopleIndex = 0;

  while (haveCandies > 0) {
    let showGive = peopleIndex + 1;

    let targetIndex = peopleIndex % num_people;

    if (showGive >= haveCandies) {
      resArr[targetIndex] += haveCandies;
      haveCandies = 0;
    } else {
      resArr[targetIndex] += showGive;
      haveCandies -= showGive;
    }

    peopleIndex++;
    // console.log(resArr);
  }

  return resArr;
};

// distributeCandies(7, 4);
module.exports = {
  name: '1103. 分糖果 II',
  testFc: distributeCandies,
};
